package com.ysd.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.ysd.Dao.Impl.NewsClassIficationDaoImpl;
import com.ysd.Dao.Impl.NewsDaoImpl;
import com.ysd.Dao.Impl.ProductSeriesDaoImpl;
import com.ysd.entity.Banner;
import com.ysd.entity.News;
import com.ysd.entity.NewsClassiIfication;
import com.ysd.entity.ProductSeries;
import com.ysd.servlet.NewsServlet;
import com.ysd.util.BaseServlet;
import com.ysd.util.Result;

public class AdminNewsServlet extends BaseServlet {
	/**
	 * 查询所有新闻 adminNews?reqName=getNews
	 * 
	 * @author 86176
	 *
	 */
	public void getNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应编码格式
		PrintWriter out = response.getWriter();
		
		int page = Integer.parseInt(request.getParameter("page"));
		int size = Integer.parseInt(request.getParameter("size"));
		int newsId = 0;
		if (request.getParameter("newsId") != null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		String newsName = request.getParameter("newsName");
		String newsTime = request.getParameter("newsTime");
		String newsContent = request.getParameter("newsContent");
		int newsCategoryId = 0;
		if (request.getParameter("newsCategoryId") != null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));
		}

		News n = new News(newsId, newsName, newsTime, newsContent, newsCategoryId, "");
		NewsDaoImpl nDao = new NewsDaoImpl();
		List<News> news = nDao.getNews(n,(page-1)*size,size);// 调用新闻实现类

	

		Gson gson = new Gson();
		String res = gson.toJson(news);// 将news转化为json字符串
		out.print(res);// 响应数据

		out.flush();
		out.close();
	}

	/**
	 * 查询所有新闻 adminNews?reqName=getNews
	 * 
	 * @author 86176
	 *
	 */
	public void getAllNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");// 设置响应编码格式
		PrintWriter out = response.getWriter();
		
		int newsId = 0;
		if (request.getParameter("newsId") != null && !request.getParameter("newsId").equals("")) {
			newsId = Integer.parseInt(request.getParameter("newsId"));
		}
		String newsName = request.getParameter("newsName");
		String newsTime = request.getParameter("newsTime");
		String newsContent = request.getParameter("newsContent");
		int newsCategoryId = 0;
		if (request.getParameter("newsCategoryId") != null && !request.getParameter("newsCategoryId").equals("")) {
			newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));
		}

		News n = new News(newsId, newsName, newsTime, newsContent, newsCategoryId, "");
		NewsDaoImpl nDao = new NewsDaoImpl();
		List<News> news = nDao.getAllNews(n);// 调用新闻实现类


		Gson gson = new Gson();
		String res = gson.toJson(news.size());// 将news转化为json字符串
		out.print(res);// 响应数据

		out.flush();
		out.close();
	}
	
	
	
	/**
	 * 删除指定新闻 adminNews?reqName=deleteNews
	 * 
	 * @author 86176
	 *
	 */
	public void deleteNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int nId = Integer.parseInt(request.getParameter("newsId"));
		NewsDaoImpl nDao = new NewsDaoImpl();// 创建实现类对象
		int n = nDao.deleteNewsById(nId);// 调用删除方法

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}
	
	
	/**
	   * 批量删除指定新闻 adminNews?reqName=deleteNewsByIds
	 * 
	 * @author 86176
	 *
	 */
	public void deleteNewsByIds(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		String nIds = request.getParameter("newsId");
		NewsDaoImpl nDao = new NewsDaoImpl();// 创建实现类对象
		int n = nDao.deleteNewsByIds(nIds);// 调用删除方法


		if (n > 0) {// 删除成功
			out.print(Result.toClient(true, "删除成功", ""));// {"success":true,"message":"删除成功","remark":""}
		} else {// 删除失败
			out.print(Result.toClient(false, "删除失败", ""));// {"success":false,"message":"删除失败","remark":""}
		}

		out.flush();
		out.close();
	}

	/**
	 * 添加新闻详情 localhost:8080/zhubao/adminProductCategory?reqName=addNew
	 * 
	 * @author 86176
	 *
	 */
	public void addNews(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDaoImpl nDao = new NewsDaoImpl();// 创建实现类对象

		String newsName = request.getParameter("newsName");
		String newsTime = request.getParameter("newsTime");
		String newsContent = request.getParameter("newsContent");
		int newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));

		News news= nDao.getNewsByName(newsName);

		if (news == null) {
			News newsList = new News(0, newsName, newsTime, newsContent, newsCategoryId);
			int n = nDao.addNews(newsList);// 调用添加产品类别方法

			if (n > 0) {// 添加成功
				out.print(Result.toClient(true, "添加成功", ""));// {"success":true,"message":"删除成功","remark":""}
			} else {// 添加失败
				out.print(Result.toClient(false, "添加失败", ""));// {"success":false,"message":"删除失败","remark":""}
			}

		} else {
			out.print(Result.toClient(false, "添加失败,新闻名称已存在", ""));
		}

		out.flush();
		out.close();

	}

	/**
	 * 根据新闻id查询新闻 adminNews?reqName=getNewsById
	 * 
	 * @author 86176
	 *
	 */
	public void getNewsById(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int newsId = Integer.parseInt(request.getParameter("nId"));

		NewsDaoImpl nDao = new NewsDaoImpl();
		News news = nDao.getNewsById(newsId);// 调用新闻实现类

		response.setCharacterEncoding("UTF-8");// 设置响应编码格式
		PrintWriter out = response.getWriter();

		Gson gson = new Gson();
		String res = gson.toJson(news);
		out.print(res);

		out.flush();
		out.close();
	}

	/**
	 * 编辑新闻 localhost:8080/zhubao/adminProductCategory?reqName=addNew
	 * 
	 * @author 86176
	 *
	 */
	public void editNews(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		NewsDaoImpl nDao = new NewsDaoImpl();// 创建实现类对象
		int newsId = Integer.parseInt(request.getParameter("newsId"));
		String newsName = request.getParameter("newsName");
		String newsTime = request.getParameter("newsTime");
		String newsContent = request.getParameter("newsContent");
		int newsCategoryId = Integer.parseInt(request.getParameter("newsCategoryId"));


			News newsList = new News(newsId, newsName, newsTime, newsContent, newsCategoryId);
			int n = nDao.editNews(newsList);

			if (n > 0) {// 成功
				out.print(Result.toClient(true, "修改成功", ""));// {"success":true,"message":"删除成功","remark":""}
			} else {// 失败
				out.print(Result.toClient(false, "修改失败", ""));// {"success":false,"message":"删除失败","remark":""}
			}

		out.flush();
		out.close();

	}

}
